package com.linefly.online_car;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.linefly.online_car.Custom.Custom_Dialog_shaixuan;
import com.linefly.online_car.Custom.MyLoading;
import com.linefly.online_car.Custom.ReLogin;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import Adapter.Adapter_baojing;
import Been.been_baojing;
import Util.DateUtil;
import Util.JsonUtil;
import Util.NavigationBarUtil;
import Util.PreferenceUtil;
import Util.StringUtil;
import Util.ToastUtil;
import okhttp3.Call;
import okhttp3.Response;

public class BaoJing extends AppCompatActivity {

    private String token_str;//用户的token
    Adapter_baojing adapter_baojing;
    RecyclerView recyclerView;
    List<been_baojing> list;
    ArrayList arrayList_detail;

    View nodata;
    Context context = BaoJing.this;
    String str_shebei,str_chepai;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bao_jing);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //  getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        }


        if (NavigationBarUtil.hasNavigationBar(this)) {
            NavigationBarUtil.initActivity(findViewById(android.R.id.content));//解决底部适配
        }

        token_str = PreferenceUtil.getString("token", "");
        str_chepai = "";
        str_shebei = "";
        nodata = (View)findViewById(R.id.nodata);
        list = new ArrayList<>();
        arrayList_detail = new ArrayList();
        recyclerView = (RecyclerView)findViewById(R.id.recycler);
        init();


    }

    public void init(){

        list.clear();
        MyLoading.show(context);
        try {
            String url = getString(R.string.url) + "/fencealarm/search";

            OkHttpUtils.get(url)
                    .connTimeOut(10000)
                    .params("token",token_str)
                    .params("device_no",str_shebei)
                    .params("car_no",str_chepai)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(String s, okhttp3.Call call, Response response) {
                            MyLoading.cancel();
                            Object object = null;
                            object = JsonUtil.jsonParse(s);
                            if(object instanceof HashMap) {
                                HashMap hashMap = (HashMap) object;
                                if (hashMap.containsKey("err")) ;
                                int err = Integer.valueOf(hashMap.get("err").toString());
                                if (err == 0) {
                                    ArrayList lists = (ArrayList) hashMap.get("list");
                                    arrayList_detail = (ArrayList) hashMap.get("list");
                                    if (lists.size() > 0) {
                                        for (int i = 0; i < lists.size(); i++) {
                                            HashMap hashMap1 = (HashMap) lists.get(i);
                                            String mc = hashMap1.get("fence_name").toString();
                                            String sb = hashMap1.get("device_no").toString();
                                            String cp = hashMap1.get("car_no").toString();
                                            String jrsj = DateUtil.getDateToString((long)Integer.valueOf(hashMap1.get("st").toString()),"yyyy-MM-dd HH:mm");
                                            String lksj = DateUtil.getDateToString((long)Integer.valueOf(hashMap1.get("ed").toString()),"yyyy-MM-dd HH:mm");
                                            String cxsj = StringUtil.secTostr((long)Integer.valueOf(hashMap1.get("ed").toString()) - (long)Integer.valueOf(hashMap1.get("st").toString()));


                                            been_baojing been = new been_baojing(mc, sb, cp, jrsj,lksj,cxsj);
                                            list.add(been);


                                        }

                                    }


                                }else if(err < 0){
                                    ReLogin.login(context);
                                }else {
                                    ToastUtil.toast(context,hashMap.get("msg").toString());
                                }

                                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(BaoJing.this);
                                recyclerView.setLayoutManager(linearLayoutManager);
                                adapter_baojing = new Adapter_baojing(list);
                                recyclerView.setAdapter(adapter_baojing);

                                adapter_baojing.setItemClick(new Adapter_baojing.item_click() {
                                    @Override
                                    public void itemclick(int position) {

                                    }
                                });


                                if(list.size() == 0){
                                    recyclerView.setVisibility(View.GONE);
                                    nodata.setVisibility(View.VISIBLE);
                                }else {
                                    recyclerView.setVisibility(View.VISIBLE);
                                    nodata.setVisibility(View.GONE);
                                }



                            }

                        }
                        @Override
                        public void onError(Call call, Response response, Exception e) {

                            Toast.makeText(BaoJing.this, "连接服务器失败", Toast.LENGTH_LONG).show();
                            e.printStackTrace();
                        }
                    });

        }catch (Exception e) {
            e.printStackTrace();
        }










    }

    public void click_shaixuan(View view){
        final Custom_Dialog_shaixuan custom_dialog_shaixuan = new Custom_Dialog_shaixuan(context);
        custom_dialog_shaixuan.show();
        custom_dialog_shaixuan.setOnClickListener(new Custom_Dialog_shaixuan.OnClickListener() {
            @Override
            public void onClick(String chepai, String shebei) {
                str_chepai = chepai;
                str_shebei = shebei;
                init();
                custom_dialog_shaixuan.cancel();

            }
        });

    }

public void click_back(View view){
        finish();
}

}
